<template>
  <div class="login-container">
    <el-form
      ref="loginFormRef"
      class="login-form"
      :model="loginForm"
      :rules="loginRules"
    >
      <div class="title-container">
        <h3 class="title">{{ $t('msg.login.title') }}</h3>
        <LangSelect class="lang-select" />
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon="user" />
        </span>
        <el-input
          v-model="loginForm.username"
          name="username"
          placeholder="username"
          type="text"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon="password" />
        </span>
        <el-input
          v-model="loginForm.password"
          :type="passwordType"
          name="password"
          placeholder="password"
        />
        <span class="show-pwd" @click="onChangepwdType">
          <span class="svg-container">
            <svg-icon
              :icon="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </span>
      </el-form-item>
      <el-button
        style="width: 100%; margin-bottom: 30px"
        type="primary"
        :loading="loading"
        @click="handlerLogin"
      >
        {{ $t('msg.login.loginBtn') }}
      </el-button>
      <div class="tips" v-html="$t('msg.login.desc')"></div>
    </el-form>
  </div>
</template>

<script setup>
  import LangSelect from '@/components/LangSelect'
  import { ref } from 'vue'
  import { validataPassword } from './rules'
  import { useI18n } from 'vue-i18n'
  import { useStore } from 'vuex'
  // 验证规则
  const i18n = useI18n()
  // 数据源
  const loginForm = ref({
    username: 'super-admin',
    password: '123456',
  })
  // 验证规则
  const loginRules = ref({
    username: [
      {
        required: true,
        message: i18n.t('msg.login.usernameRule'),
        trigger: 'blur',
      },
      //{ min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' },
    ],
    password: [
      {
        required: true,
        trigger: 'blur',
        validator: validataPassword(),
      },
      //{ min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
    ],
  })
  // 密码显示隐藏
  const passwordType = ref('password')
  const onChangepwdType = () => {
    // 使用ref生成的数据，在script中使用时，需要使用.value来获取，但是在template中可以直接使用
    passwordType.value = passwordType.value === 'password' ? 'text' : 'password'
  }
  // 登录
  const loading = ref(false)
  const store = useStore() // 获取store实例
  const loginFormRef = ref(null) // 获取表单实例
  const handlerLogin = () => {
    //进行表单校验
    loginFormRef.value.validate(async (valid) => {
      if (valid) {
        // 触发登录动作
        // 进行登陆后处理
        loading.value = true
        store
          .dispatch('user/login', loginForm.value)
          .then((res) => {
            loading.value = false
            // 登录成功后，跳转到首页
          })
          .catch((err) => {
            console.log(err)
            loading.value = false
          })
      }
    })
  }
</script>

<style lang="scss" scoped>
  $bg: #2d3a4b;
  $dark_gray: #889aa4;
  $light_gray: #eee;
  $cursor: #fff;

  .login-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;

    .login-form {
      position: relative;
      width: 520px;
      max-width: 100%;
      padding: 160px 35px 0;
      margin: 0 auto;
      overflow: hidden;

      :deep(.el-form-item) {
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        color: #454545;
      }

      :deep(.el-input) {
        display: inline-block;
        height: 47px;
        width: 85%;

        input {
          background: transparent;
          border: 0px;
          --webkit-appearance: none;
          border-radius: 0px;
          padding: 12px 5px 12px 15px;
          color: $light_gray;
          height: 47px;
          caret-color: $cursor;
        }
      }

      .tips {
        font-size: 16px;
        color: white;
        line-height: 24px;
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;
    }

    .title-container {
      position: relative;

      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
      ::v-deep .lang-select {
        position: absolute;
        right: 0;
        top: 4px;
        background-color: #fff;
        font-size: 22px;
        padding: 4px;
        border-radius: 4px;
        cursor: pointer;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
</style>
